export default {
  data() {
    return {
      downloadInterface: '', // 下载模版
      exportInterface: '', // 批量导出
      importInterface: '', // 批量导入

      fileList: [],
      downloadLoading: false,
      importLoading: false
    }
  },
  methods: {
    /**
     * 下载模板 批量导出
     */
    downloadTemplate(param, name) {
      this.downloadLoading = true
      this.downloadInterface(param).then(res => {
        this.downloadLoading = false
        this.downloadFileFun(res, (this.name || '') + name + '.xlsx')
      })
    },

    // /**
    //  * 批量导入 上传
    //  */
    // fileChange(files) {
    //   this.importLoading = true
    //
    //   this.fileList = []
    //   this.fileList.push(files.raw)
    //   const file = new FormData()
    //   file.append('file', files.raw)
    //   this.importInterface(file).then((res) => {
    //     this.importLoading = false
    //     this.getData()
    //     if (res.size) {
    //       this.$message.error('导入失败，错误数据请重新修改')
    //       this.downloadFileFun(res, this.name + '.xlsx')
    //     } else {
    //       this.$message.success('导入成功')
    //     }
    //   }).catch(() => {
    //     this.importLoading = false
    //   })
    // },

    /**
     * 下载文件
     * @param res 资源
     * @param name 文件名称
     */
    downloadFileFun(res, name) {
      const blob = new Blob([res], {
        type: `${res.type}charset=utf-8`
      })
      const downloadLink = document.createElement('a')
      const URL = window.URL || window.webkitURL || window.moxURL
      const downUrl = URL.createObjectURL(blob)
      downloadLink.href = downUrl
      downloadLink.download = name
      downloadLink.click()
      window.URL.revokeObjectURL(downUrl)
    }
  }
}

